from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from exts import db
from app import app
from apps.cms import  models as cms_models
from apps.front import  models as front_modes
from apps.models import BannerModels,BoardModels,PostModels

CMSuser = cms_models.CMSUser
CMSrole = cms_models.CMSRole
CMSpermission = cms_models.CMSPermission

FrontUser = front_modes.FrontUser

manage = Manager(app)
migrate = Migrate(app,db)
manage.add_command("db",MigrateCommand)

@manage.option("-u","--username",dest="username")
@manage.option("-p","--password",dest="password")
@manage.option("-e","--email",dest="email")
def creat_cms_users(username,password,email):
    user = CMSuser(username=username,password=password,email=email)
    db.session.add(user)
    db.session.commit()
    print("用户添加完成")


@manage.option("-u","--username",dest="username")
@manage.option("-t","--telephone",dest="telephone")
@manage.option("-p","--password",dest="password")
def creat_front_users(telephone,username,password):
    user = FrontUser(telephone=telephone,username=username,password=password)
    # print(password)
    db.session.add(user)
    db.session.commit()
    print("用户添加完成")



@manage.option("-e","--email",dest="email")
@manage.option("-n","--name",dest="name")
def add_permission(email,name):
    user = CMSuser.query.filter(CMSuser.email == email).first()
    # print(user)
    if user:
        role = CMSrole.query.filter(CMSrole.name == name).first()
        # print(role)
        if role:
            role.users.append(user)
            db.session.commit()
            print("权限添加成功")
        else:
            print("没有%s这个角色" % role)
    else:
        print("%s 这个邮箱错误" % email)



@manage.command
def creat_role():
    #1、访问者（修改个人信息）
    # visitor = CMSrole(name="访问者",desc="只能访问数据，没有修改权限")
    visitor = CMSrole(name="访问者",desc="只能访问数据，没有修改权限")

    visitor.permissions = CMSpermission.VISITOR
    #2、运营人员（修改个人信息,管理帖子，管理评论,管理评论）

    operator = CMSrole(name="运营人员",desc="管理帖子,管理评论,管理前台用户")
    operator.permissions = CMSpermission.VISITOR | CMSpermission.COMMENTER \
                           | CMSpermission.CMSUSER |CMSpermission.POSTER |CMSpermission.FRONTUSER

    #3、管理员（拥有绝大部分权限）
    admin = CMSrole(name="管理员",desc="拥有本系统的所有权限")
    admin.permissions = CMSpermission.VISITOR | CMSpermission.POSTER | CMSpermission.CMSUSER \
                        | CMSpermission.BOAEDER| CMSpermission.FRONTUSER | CMSpermission.COMMENTER

    #4、开发者
    developer = CMSrole(name="开发人员",desc="开发者专用权限")
    developer.permissions = CMSpermission.ALL_PERMISSION

    db.session.add_all([visitor,operator,admin,developer])
    db.session.commit()

@manage.command
def check_permission():
    user = CMSuser.query.first()
    if user.has_permission(CMSpermission.VISITOR):
        return "用户已经拥有访问权限！"
    else:
        return "用户暂时还没有访问权限！"


@manage.command
def create_test_posts():
    for x in range(100):
        title = "标题%s" %x
        context = "内容: %s" %x
        author = FrontUser.query.first()
        board = BoardModels.query.first()
        post = PostModels(title=title,context=context)
        post.board = board
        post.author =author
        db.session.add(post)
        db.session.commit()
    print("恭喜！测试帖子添加成功")

if __name__ == '__main__':
    manage.run()
